package com.sfjswl.fp.common.enums;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public enum NodeTypeEnum {
    NONE((byte) 0),

    AND((byte) 1),

    TRUE((byte) 2),

    ALL((byte) 3),

    ANY((byte) 4),

    LEAF_FLOW((byte) 5),

    LEAF_RESULT((byte) 6),

    LEAF_NONE((byte) 7),
    P_NONE((byte) 8),

    P_AND((byte) 9),

    P_TRUE((byte) 10),

    P_ALL((byte) 11),

    P_ANY((byte) 12);

    private static final Map<Byte, NodeTypeEnum> MAP = new HashMap<>(NodeTypeEnum.values().length);

    private static final Set<Byte> RELATION_SET = new HashSet<>(10);

    static {
        for (NodeTypeEnum enums : NodeTypeEnum.values()) {
            MAP.put(enums.getType(), enums);
        }
        RELATION_SET.add(NONE.type);
        RELATION_SET.add(AND.type);
        RELATION_SET.add(TRUE.type);
        RELATION_SET.add(ALL.type);
        RELATION_SET.add(ANY.type);
        RELATION_SET.add(P_NONE.type);
        RELATION_SET.add(P_AND.type);
        RELATION_SET.add(P_TRUE.type);
        RELATION_SET.add(P_ALL.type);
        RELATION_SET.add(P_ANY.type);
    }

    private final byte type;

    NodeTypeEnum(byte type) {
        this.type = type;
    }

    public static NodeTypeEnum getEnum(Byte type) {
        if (type == null) {
            return null;
        }
        return MAP.get(type);
    }

    public byte getType() {
        return type;
    }

    public static boolean isRelation(byte type) {
        return RELATION_SET.contains(type);
    }

    public static boolean isLeaf(byte type) {
        return type == LEAF_FLOW.type || type == LEAF_NONE.type || type == LEAF_RESULT.type;
    }
}
